﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.Common;

namespace MicroDal
{
    internal class Tracer
    {
        private static TraceSource _trace = new TraceSource("MicroDal");

        public static void StartOperation(string text)
        {
            _trace.TraceEvent(TraceEventType.Information, EventID.StartOperation, text);
        }

        public static void EndOperation(string text)
        {
            _trace.TraceEvent(TraceEventType.Information, EventID.EndOperation, text);
        }

        public static void CtorCalled(string text)
        {
            _trace.TraceEvent(TraceEventType.Verbose, EventID.Trace, text);
        }

        /// <summary>
        /// Writes information message to the tracelisteners.
        /// </summary>
        public static void WriteMessage(string format, params object[] args)
        {
            _trace.TraceEvent(TraceEventType.Information, EventID.Trace, format, args);
        }


        /// <summary>
        /// Writes verbose message to tracelisteners.
        /// </summary>
        public static void WriteTrace(string format, params object[] args)
        {
            _trace.TraceEvent(TraceEventType.Verbose, EventID.Trace, format, args);
        }

        public static void WriteParametersIfVerbose(object[] parameters)
        {
            if (_trace.Switch.Level >= SourceLevels.Verbose || _trace.Switch.Level == SourceLevels.All)
            {
                if (parameters != null && parameters.Length > 0)
                {
                    foreach (var item in parameters)
                    {
                        _trace.TraceEvent(TraceEventType.Verbose, EventID.Trace, "{0}", item);
                    }
                }
            }
        }

        public static void WriteException(Exception ex)
        {
            _trace.TraceData(TraceEventType.Error, EventID.UnhandledException, ex);
        }

        class EventID
        {
            public static int Trace = 0;
            public static int StartOperation = 1;
            public static int EndOperation = 2;
            public static int UnhandledException = 3;
            public static int Exception = 4;
        }
    }
}
